package com.saclub.app.fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import butterknife.OnClick;

import com.loopj.android.http.RequestParams;
import com.offroader.share.wxapi.ShareUtils;
import com.offroader.utils.JsonUtils;
import com.offroader.utils.ViewUtils;
import com.saclub.app.R;
import com.saclub.app.bean.part.News;
import com.saclub.app.common.AccessTokenKeeper;
import com.saclub.app.http.ApiRequest;

public class MoreDialogFragment extends BaseProgressBottomDialogFragment {

	private Bundle args;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

		//无标题
		getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);

		return super.onCreateView(inflater, container, savedInstanceState);
	}

	@Override
	protected int obtainContentRes() {
		return R.layout.frag_more_dialog;
	}

	@Override
	protected void initParams() {

		args = getArguments();

	}

	@Override
	protected void initView() {

	}

	@Override
	protected void initData() {

	}

	@OnClick(R.id.id_share_to_weichat_friend)
	public void shareToWechatFriend() {

		String newsJson = args.getString("news", "");
		News news = JsonUtils.fromJson(newsJson, News.class);

		String appId = getString(R.string.weichat_app_id);
		String appSecret = getString(R.string.weichat_app_secret);

		String title = news.getNewTitle();
		String content = news.getNewDesc();
		String url = news.getNewUrl();
		String imgUrl = news.getNewImage();

		//		ViewUtils.toast(title);

		ShareUtils.shareToWeiChat(getActivity(), appId, appSecret, title, content, imgUrl, url);
	}

	@OnClick(R.id.id_share_to_weichat_circle)
	public void shareToWechatCircle() {

		String newsJson = args.getString("news", "");
		News news = JsonUtils.fromJson(newsJson, News.class);

		String appId = getString(R.string.weichat_app_id);
		String appSecret = getString(R.string.weichat_app_secret);

		String title = news.getNewTitle();
		String content = news.getNewDesc();
		String url = news.getNewUrl();
		String imgUrl = news.getNewImage();

		//		ViewUtils.toast(title);

		ShareUtils.shareToWeiChatCircle(getActivity(), appId, appSecret, title, content, imgUrl, url);

	}

	@OnClick(R.id.id_add_to_like)
	public void addToLike() {

		String newsJson = args.getString("news", "");
		News news = JsonUtils.fromJson(newsJson, News.class);

		AccessTokenKeeper tokenKeeper = AccessTokenKeeper.readAccessToken(getActivity());

		if (tokenKeeper.isLogin()) {

			RequestParams params = new RequestParams();
			params.put("webuser_id", tokenKeeper.getOpenId());
			params.put("news_id", news.getNewId());

			ApiRequest.userAddLike.request(this, params);

		} else {
			AccountLoginFragment.go(getActivity());
		}

	}

	@OnClick(R.id.id_close_dialog)
	public void closeDialog() {
		getDialog().dismiss();
	}

}
